import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/11 15:11
 * @description 自定义排序
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int N = sc.nextInt(), type = sc.nextInt();
            sc.nextLine();
            List<People> list = new ArrayList<>();
            for (int i = 0; i < N; i++) {
                String t = sc.nextLine();
                String[] info = t.split(" ");
                list.add(new People(info[0], Integer.parseInt(info[1]), i));
            }
            Collections.sort(list, (a, b) -> {
                if (a.grades != b.grades) {
                    if (type == 0) {
                        return b.grades - a.grades;
                    }
                    return a.grades - b.grades;
                }
                return a.idx - b.idx;
            });
            for (People p : list) {
                System.out.print(p.name + " " + p.grades + "\n");
            }
        }
    }
}
class People{
    String name;
    int grades;
    int idx;
    public People(String n, int g, int i) {
        this.name = n;
        this.grades = g;
        this.idx = i;
    }
}
